package cn.ubeta.b2c.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.ubeta.b2c.R;

/**
 * Created by denny on 2016/10/27.
 */

public class ItemView extends RelativeLayout {

    private TextView mTvLabel;
    private TextView mTvDesc;
    private TextView mTvDesc2;
    private ImageView mIvLeft;

    public ItemView(Context context) {
        this(context, null);
    }

    public ItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.widget_item_view, this);
        init(context, attrs);
    }

    /**
     * 初始化控件
     * @param attrs
     */
    private void init(Context context,AttributeSet attrs) {
        mTvLabel = (TextView) findViewById(R.id.tv_label);
        mTvDesc = (TextView) findViewById(R.id.tv_desc);
        mTvDesc2 = (TextView) findViewById(R.id.tv_desc2);
        mIvLeft = (ImageView) findViewById(R.id.iv_left);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ItemView);

        String label = a.getString(R.styleable.ItemView_iv_label);
        String desc = a.getString(R.styleable.ItemView_iv_desc);
        int padding = (int) a.getDimension(R.styleable.ItemView_iv_padding, 0);

        // 标签
        if (TextUtils.isEmpty(label)) {
            label = "错误";
        }
        mTvLabel.setText(label);

        // 描述
        if (!TextUtils.isEmpty(desc)) {
            mTvDesc.setText(desc);
            mTvDesc.setVisibility(VISIBLE);
        }

        // padding
        if (padding > 0) {
            findViewById(R.id.rl_item_view).setPadding(padding, padding, padding, padding);
        }
    }

    /**
     * 更新描述
     * @param desc
     */
    public void updateDesc(String desc) {
        updateDesc(desc, null);
    }

    public void updateDesc(String desc, String desc2) {
        updateDesc(desc, desc2, 0);
    }

    public void updateDesc(String desc, String desc2, int resImg) {
        // 第一行描述
        if (TextUtils.isEmpty(desc)) {
            mTvDesc.setVisibility(GONE);
        } else {
            mTvDesc.setText(desc);
            mTvDesc.setVisibility(VISIBLE);
        }
        // 第二行描述
        if (TextUtils.isEmpty(desc2)) {
            mTvDesc2.setVisibility(GONE);
        } else {
            mTvDesc2.setText(desc2);
            mTvDesc2.setVisibility(VISIBLE);
        }
        // 左边图标
        if (resImg == 0) {
            mIvLeft.setImageDrawable(null);
            mIvLeft.setVisibility(GONE);
        } else {
            //mIvLeft.setImageURI(Uri.parse("res:///" + resImg));
            mIvLeft.setImageResource(resImg);
            mIvLeft.setVisibility(VISIBLE);
        }
    }
}
